package com.yylending.las.personal.bandcard;

import android.text.TextUtils;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by ying_he on 2017/7/7.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class CardBinPresenter extends Presenter<CardBinModel, CardBinPresenter.View> {
    private final static String TAG = CardBinPresenter.class.getSimpleName();
    private static final String MSG_GET_CARD_BIN_FAILED = "未能识别该银行卡，请重试";
    private static final String MSG_NOT_SUPPORT_CREDIT_CARD = "暂不支持信用卡";
    private static final String MSG_NOT_SUPPORT_BANK = "暂不支持该银行卡";

    /**
     * 不支持信用卡
     */
    private static final String RESULT_CODE_NOT_SUPPORT_CREDIT_CARD = "100050";
    /**
     * 不支持的银行
     */
    private static final String RESULT_CODE_NOT_SUPPORT_BANK = "100051";
    /**
     * 未找到卡bin信息
     */
    private static final String RESULT_CODE_NOT_CARD_BIN = "100052";

    // 点击绑定按钮后获取卡bin信息, 获取成功后直接调绑卡接口.
    public static final int FUNCTION_CODE_CARD_BIN_FROM_BIND_CARD_BTN_CLICKED = 100;
    // 单纯只是获取卡bin信息.
    public static final int FUNCTION_CODE_CARD_BIN_FROM_OTHERS = 101;

    private View mView;
    private String mRequestPage;

    public static class Generator implements PresenterFactory<CardBinPresenter> {
        @Override
        public CardBinPresenter create() {
            return new CardBinPresenter();
        }
    }

    public interface View extends IView {
        void getCardBinSuccess(CardBinModel.CardBin cardBin, int functionCode);
        void getCardBinFailed(String msg);
    }

    @Override
    public void attach(CardBinPresenter.View view) {
        mRequestPage = view.getClass().getName();
        mView = view;
        mModel = new CardBinModel();
        EventAgent.register(this);
    }

    @Override
    public void detach() {
        mView = null;
        mModel = null;
        EventAgent.unregister(this);
    }

    public void getCardBin(String bankCardNo, int functionCode) {
        execute(new AESEntity(new CardBinModel.RequestParams(bankCardNo)), functionCode, true, null, mRequestPage);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getCardBinResult(CardBinModel.CardBinResult result) {
        if (mRequestPage.equals(result.requestPage)) {
            if (BaseResultEntity.isResponseSuccess(result)) {
                CardBinModel.CardBin cardBin = result.getData();
                if (cardBin != null) {
                    mView.getCardBinSuccess(cardBin, result.requestCode);
                    return;
                }
                mView.getCardBinFailed(TextUtils.isEmpty(result.getRetMsg()) ? MSG_GET_CARD_BIN_FAILED : result.getRetMsg());
            } else if (TextUtils.equals(RESULT_CODE_NOT_SUPPORT_CREDIT_CARD, result.getRetCode())) {
                mView.getCardBinFailed(TextUtils.isEmpty(result.getRetMsg()) ? MSG_NOT_SUPPORT_CREDIT_CARD : result.getRetMsg());
            } else if (TextUtils.equals(RESULT_CODE_NOT_SUPPORT_BANK, result.getRetCode())) {
                mView.getCardBinFailed(TextUtils.isEmpty(result.getRetMsg()) ? MSG_NOT_SUPPORT_BANK : result.getRetMsg());
            } else if (TextUtils.equals(RESULT_CODE_NOT_CARD_BIN, result.getRetCode())) {
                mView.getCardBinFailed(TextUtils.isEmpty(result.getRetMsg()) ? MSG_GET_CARD_BIN_FAILED : result.getRetMsg());
            } else {
                mView.getCardBinFailed(TextUtils.isEmpty(result.getRetMsg()) ? MSG_GET_CARD_BIN_FAILED : result.getRetMsg());
            }
        }
    }
}
